package com.aifeng.hassan_food.login

import android.os.Bundle
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.base.oneClick
import com.aifeng.common_ui.helper.*
import com.aifeng.hassan_food.R
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.LoginViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.json.JSONObject
import org.koin.androidx.viewmodel.ext.android.getViewModel

class LoginActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        setTransparentAndToolbar(isDarkFont = false)
        initTitle()
    }

    override fun getViewModel() = getViewModel<LoginViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        if (getString("phone").isNotBlank()) {
            login_account.setText(getString("phone"))
            login_account.setSelection(login_account.text.length)
        }

        login_forget.oneClick { startActivity<ForgetActivity>() }
        bt_register.oneClick { startActivity<RegisterCodeActivity>() }
    }

    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_login -> {
                when {
                    login_account.text.isBlank() -> {
                        login_account.requestFocus()
                        toast("请输入手机号")
                        return
                    }
                    login_pwd.text.isBlank() -> {
                        login_pwd.requestFocus()
                        toast("请输入登录密码")
                        return
                    }
                    !login_account.text.isMobile() -> {
                        login_account.requestFocus()
                        toast("请输入正确的手机号")
                        return
                    }
                    !login_pwd.text.isPasswordWithAndNumber() -> {
                        toast("请输入6~20位字母加数字组合密码")
                        return
                    }
                }

                getViewModel().login(
                    login_account.textString(),
                    login_pwd.textString()
                ).bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = {
                            val obj = JSONObject(it)
                            putShared("phone", obj.optNotNull("phone"))
                            putShared("userId", obj.optNotNull("id"))
                            putShared("nickName", obj.optNotNull("nick"))
                            putShared("shopName", obj.optNotNull("shopname"))
                            putShared("headImg", obj.optNotNull("headimg"))
                            putShared("shopId", obj.optNotNull("shopid"))
                            putShared("shopCode", obj.optNotNull("shopqrcode"))
                            putShared("balance", obj.optNotNull("balance"))
                            putShared("openId", obj.optNotNull("wx_open_id"))

                            putShared("realName", obj.optNotNull("realname"))
                            putShared("bankCard", obj.optNotNull("bankCardNumber"))
                            putShared("bindId", obj.optNotNull("bind_wx_id"))
                            putShared("bindImg", obj.optNotNull("bind_wx_img"))
                            putShared("bindName", obj.optNotNull("bind_wx_nick"))

                            putShared("createData", obj.optNotNull("upgradedate"))

                            isLogin = true
                            LiveEventBus.get("loginStatus").post("login")
                            LiveEventBus.get("mainLogin").post("refresh")
                            LiveEventBus.get("otherLogin").post("refresh")
                            ActivityStack.screenManager.popActivities(this@LoginActivity::class.java)
                        }
                    )
            }
        }
    }

}
